package com.klasster.recruitmentPlatform.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.klasster.recruitmentPlatform.common.response.Result;
import com.klasster.recruitmentPlatform.common.response.ResultCode;
import com.klasster.recruitmentPlatform.domain.po.Admin;
import com.klasster.recruitmentPlatform.domain.po.Company;
import com.klasster.recruitmentPlatform.domain.po.Job;
import com.klasster.recruitmentPlatform.domain.po.Recruiter;
import com.klasster.recruitmentPlatform.domain.vo.JobVO;
import com.klasster.recruitmentPlatform.service.impl.AdminServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.CompanyServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.JobServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.RecruiterServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/admin")
public class AdminController {

    private final JobServiceImpl jobService;
    private final CompanyServiceImpl companyService;
    private final AdminServiceImpl adminService;
    private final RecruiterServiceImpl recruiterService;

    @GetMapping("/getAdmin/{admin_id}")
    public Result getAdmin(@PathVariable Long admin_id) {
        Admin admin = adminService.getById(admin_id);
        return Result.success(ResultCode.ADMIN_GET_SUCCESS,admin);
    }

    @Transactional
    @PutMapping("/updateAdmin")
    public Result updateAdmin(@RequestBody Admin admin) {
        return Result.success(ResultCode.ADMIN_SAVE_SUCCESS,adminService.updateById(admin));
    }

    /**
     * 获取未审核的招聘信息
     */
    @GetMapping("/getUnauditedJobPosting")
    public Result getUnauditedJobPosting() {
        //只返回state为0的招聘信息
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",0);
        List<JobVO> list = BeanUtil.copyToList(jobService.list(queryWrapper),JobVO.class);
        list.forEach(e -> {
            Company company = companyService.getById(e.getCompany_id());
            e.setCompany_name(company.getCompany_name());
        });
        return Result.success(ResultCode.ADMIN_GET_SUCCESS,list);
    }

    /**
     * 获取审核通过的招聘信息
     */
    @GetMapping("/getAuditedJobPosting")
    public Result getAuditedJobPosting() {
        //只返回state为1的招聘信息
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",1);
        List<JobVO> list = BeanUtil.copyToList(jobService.list(queryWrapper),JobVO.class);
        list.forEach(e -> {
            Company company = companyService.getById(e.getCompany_id());
            e.setCompany_name(company.getCompany_name());
        });
        return Result.success(ResultCode.ADMIN_GET_SUCCESS,list);
    }

    /**
     * 通过招聘信息
     */
    @Transactional
    @PutMapping("/agreeJobPosting")
    public Result agreeJobPosting(@RequestParam Long job_id) {
        UpdateWrapper<Job> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("job_id",job_id).set("state", 1);
        return Result.success(ResultCode.ADMIN_SAVE_SUCCESS,jobService.update(updateWrapper));
    }

    /**
     * 不通过招聘信息
     */
    @Transactional
    @PutMapping("/disagreeJobPosting")
    public Result disagreeJobPosting(@RequestParam Long job_id) {
        UpdateWrapper<Job> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("job_id",job_id).set("state", 2);
        return Result.success(ResultCode.ADMIN_SAVE_SUCCESS,jobService.update(updateWrapper));
    }

    @Transactional
    @PutMapping("/deleteJobPosting")
    public Result deleteJobPosting(@RequestParam Long job_id) {
        UpdateWrapper<Job> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("job_id",job_id).set("state", 3);
        return Result.success(ResultCode.ADMIN_SAVE_SUCCESS,jobService.update(updateWrapper));
    }


    /**
     * 获取未审核的企业信息
     */
    @GetMapping("/getUnauditedCompany")
    public Result getUnauditedCompany() {
        //只返回state为0的招聘信息
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",0);
        List<Company> list = companyService.list(queryWrapper);
        return Result.success(ResultCode.ADMIN_GET_SUCCESS,list);
    }

    /**
     * 获取审核通过的企业信息
     */
    @GetMapping("/getAuditedCompany")
    public Result getAuditedCompany() {
        //只返回state为1的招聘信息
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",1);
        List<Company> list = companyService.list(queryWrapper);
        return Result.success(ResultCode.ADMIN_GET_SUCCESS,list);
    }

    /**
     * 通过企业信息
     */
    @Transactional
    @PutMapping("/agreeCompany")
    public Result agreeCompany(@RequestParam Long company_id) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",company_id);
        Company company = companyService.getOne(queryWrapper);
        //更新企业所有者的企业id
        UpdateWrapper<Recruiter> recruiterUpdateWrapper = new UpdateWrapper<>();
        recruiterUpdateWrapper.eq("recruiter_id",company.getOwner_id());
        recruiterUpdateWrapper.set("company_id",company_id);
        recruiterService.update(recruiterUpdateWrapper);
        //更新企业状态
        company.setState(1);
        return Result.success(ResultCode.ADMIN_SAVE_SUCCESS,companyService.updateById(company));
    }

    /**
     * 不通过企业信息
     */
    @Transactional
    @PutMapping("/disagreeCompany")
    public Result disagreeCompany(@RequestParam Long company_id) {
        UpdateWrapper<Company> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("company_id",company_id).set("state", 2);
        return Result.success(ResultCode.ADMIN_SAVE_SUCCESS,companyService.update(updateWrapper));
    }

    @Transactional
    @PutMapping("/deleteCompany")
    public Result deleteCompany(@RequestParam Long company_id) {
        UpdateWrapper<Company> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("company_id",company_id).set("state", 2);
        return Result.success(ResultCode.ADMIN_SAVE_SUCCESS,companyService.update(updateWrapper));
    }
}
